/*
cmdline.cpp

xssbuster - a XSS exploit filter
Copyright (C) 2006 Jeffrey Davis

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

#include "cmdline.h"

CommandLineProcessor::CommandLineProcessor()
{
	
}

CommandLineProcessor::~CommandLineProcessor()
{
	
}

int CommandLineProcessor::Parse(int argc, char const* argv[])
{
	bool lastIsOption = false;
	char *pos;
	std::string option, value;
	
	try
	{
		// Loop through all command line arguments
		for(int i=1; i < argc; i++)
		{
			// Retrieve the command line argument at the current position
			pos = (char *)argv[i];
			
			if (pos[0] == '-') // Does the argument start with a dash?
			{ 
				// Yes, it is an option. Check to see if the last argument was an option
				if(lastIsOption == false)
				{
					// The last argument was not an option, so this argument must be a new option.
					option.assign(pos);
					lastIsOption = true;
				}
				else
				{
					// The last argument was an option, so it must have been an option
					// without a value. Assign the option with the value of "on".
					options[option] = "on";
					lastIsOption = true;
					option.assign(pos);
				}
			}
			else  
			{
				// No, it is a value. Check to see if the last argument was an option
				if(lastIsOption == true)
				{
					// The last argument was an option, so this is an option/value pair.
					value.assign(pos);
					lastIsOption = false;
					options[option] = value;
				}
				else
				{
					// The last argument was not an option, disregard the last value and
					// assign this value to the option "argument".
					value.assign(pos);
					options["argument"] = value;
				}
			}
		}
		
		// End of the loop
		
		if(lastIsOption == true) // Was there a dangling option?
		{
			// The option must be an option without a value. Assign the
			// option with the value of "on".
			options[option] = "on";
			lastIsOption = true;
			option.assign(pos);
		}
	}
	catch(std::exception e)
	{
		return (int)false;
	}
	
	
	return (int)true;
}

int CommandLineProcessor::ModifyOption(std::string option, std::string value)
{
	try
	{
		// Set the option with the value.
		options[option] = value;
	}
	catch(std::exception e)
	{
		return (int)false;
	}
	return (int)true;
}

std::string CommandLineProcessor::RetrieveOption(std::string option)
{
	try
	{
		// Return the value of the option.
		return options[option];
	}
	catch(std::exception e)
	{
		return "";
	}
}